﻿using ReactiveUI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WimForms_MVVM模式.ViewModel;

namespace WimForms_MVVM模式
{
    public partial class Form1 : Form, IViewFor<PersonViewModel>
    {
        //需要绑定的ViewModel类
        object IViewFor.ViewModel
        {
            get => ViewModel;
            set => ViewModel = (PersonViewModel)value;
        }

        /// <summary>
        /// 任务信息的类型
        /// </summary>
        public PersonViewModel ViewModel { get; set; }

        public Form1()
        {
            InitializeComponent();
            //设置绑定对象字段的关系
            this.WhenActivated(a =>
            {
                //this.Bind表示双向绑定:3个textBox分别双向绑定ViewModel对应的属性字段用作显示及输入更新
                a(this.Bind(ViewModel, vm => vm.Id, v => v.textBox1.Text));
                a(this.Bind(ViewModel, vm => vm.Name, v => v.textBox2.Text));
                a(this.Bind(ViewModel, vm => vm.Age, v => v.textBox3.Text));
                //this.OneWayBind表示单向绑定:3个label分别单向绑定ViewModel对应的属性字段用做显示
                a(this.OneWayBind(ViewModel, vm => vm.Id, v => v.label1.Text));
                a(this.OneWayBind(ViewModel, vm => vm.Name, v => v.label2.Text));
                a(this.OneWayBind(ViewModel, vm => vm.Age, v => v.label3.Text));
            });

            //实例化任务
            ViewModel = new PersonViewModel();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ViewModel.Id = 123;
            ViewModel.Name = "老王";
            ViewModel.Age = 38;
        }
    }
}
